import Object3D from '@/Scene/Object3D';
import Renderer from "@/Render/Render";
import Camera from "./Camera";
import QuadtreePrimitive from "./QuadtreePrimitive";
// @ts-ignore
import GltfParser from '@/util/Gltf/GLTFParser'
export default class Scene {
    private _camera: Camera;
    public _quadtreePrimitive: QuadtreePrimitive;
    private _canvas: HTMLCanvasElement;
    private _render: Renderer;
    // private object3Ds: Object3D[] = [];
    /**
     *
     */
    constructor(canvas: HTMLCanvasElement) {
        this._canvas = canvas;
        this._camera = new Camera(this);

        this._quadtreePrimitive = new QuadtreePrimitive(this._camera);
        let tiles = this._quadtreePrimitive.TilesToRender;
        // console.log(0.1 + 0.2);
        this._render = new Renderer(this._canvas, tiles, this);
        this._render.init().then(() => {
            fetch('./triangle.json').then(res => res.json()).then(data => {
                let gltf = new GltfParser(data);
                let object3D = new Object3D(this._render, gltf.getIndices(), gltf.getVertices());
                this._render.toDrawObject3Ds.push(object3D);
                this.startRenderLoop()
            })

        })
    }
    //尽量保持60帧率
    startRenderLoop() {
        //当前帧需要做的事情
        this._render.toDrawTiles = this._quadtreePrimitive.TilesToRender;
        this._render.render();
        //下一帧需要做的，形成渲染循环
        requestAnimationFrame(() => {
            this.startRenderLoop();
        })
    }
    get canvas(): HTMLCanvasElement {
        return this._canvas;
    }

    get camera(): Camera {
        return this._camera;
    }

}